
$(document.body).mouseup(function(){
    ref = getRef();
    //debug(ref);
    /*domWalker(document.getElementById("2_ne/2/1"),document.getElementById("2_ne/2/3"),boldMe);
    /*highlightRef(ref, 'highlight');

     */
}); 

function getRef() {
    wSel = window.getSelection();
    fPath = "";
    node = $(wSel.anchorNode);
    parents = node.parents();
    for (int i = 0; i < parents.length; i++){
		
    }


	}

   function domWalker(begNode, endNode, func){
       console.debug(begNode.toString());
       if (begNode == endNode) {
	   console.debug("got to end!");
	   return;
       }
       else if (begNode.firstChild) {
	   console.debug("FIRSTCHILD!");
	   
	   domWalker(begNode.firstChild,endNode,func);}

	   else if (begNode.nextSibling) {
	    	console.debug("nextsibling");   
	       domWalker(begNode.nextSibling,endNode,func);
	   }
       else {
	   while (!begNode.parentNode.nextSibling){
	       begNode = begNode.parentNode
	   }
	   console.debug("going up!");
	   if (begNode.parentNode.nextSibling) domWalker(begNode.parentNode.nextSibling,endNode,func);
       
       }
       func(begNode);

   }

   function boldMe(el){
   $(el).contents().not("[@nodeType=1]").wrap("<b/>");
   }



    
    function highlightRef(ref, hclass) {
	tagA = ref.book + "/" + ref.chapter + "/" + ref.start.verse;
	tagB = ref.book + "/" + ref.chapter + "/" + ref.end.verse;
	nodeA = document.getElementById(tagA);
	nodeAclone = nodeA.cloneNode(true);
	nodeAwords = nodeAclone.textContent.split(' ');
	textContent = "";
	for (i = 0; i < nodeAwords.length; i++) {
	    	if (i = ref.start.word )
		textContent += nodeAwords[i]; 

	}
	
	nodeA = nodeAclone;
	
/*	

	).textNodes(true).wrap("<span class='" + hclass + "'></span>");
	
	*/

    }
    
    function getTextNodes(ref) {
	

    }

    function countWordsUntil(text, match) {
	tArray = text.split(' ');
	wordNum = 0
	for (i = 0; i < tArray.length; i++) {
		if (tArray[i] == match) return i;
	    }
	    return null;
    }

    function debug(ref) {
	console.debug("Debug Info:  Domain:" + ref.domain + ", language:"  + ref.language + ", book:" + ref.book + ", chapter:" + ref.chapter + "\nstartverse:" + ref.start.verse + ", startword:" + ref.start.word + "\nendverse:" + ref.end.verse + ", endword:" + ref.end.word + "\n");

    }
